//
//  LeftViewController.m
//  瞬间
//
//  Created by dlios on 15-6-30.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "LeftViewController.h"
#import "HTTPTool.h"
#import "adaptModel.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
@interface LeftViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *Arr;
@property (nonatomic, retain) UIView *headView;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UIScrollView *scrollView;

@end

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self openDrawerButton];
    [self createTableView];
    [self createData];
    
    
    
    
    
}
// 开启抽屉button
- (void)openDrawerButton
{
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(10, 20, 40, 40);
    [self.button setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
    [self.view addSubview:self.button];
    [self.button addTarget:self action:@selector(openDrawer:) forControlEvents:UIControlEventTouchUpInside];
}


- (void)openDrawer:(UIButton *)button
{
    [self.drawer open];
}

- (void)drawerControllerWillOpen:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = NO;
}

- (void)drawerControllerDidClose:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = YES;
}

// 首页
- (void)createTableView
{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    self.headView.backgroundColor = [UIColor lightGrayColor];
    self.tableView.tableHeaderView = self.headView;
    [_headView release];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    cell.textLabel.text = @"1";
    return cell;
}

// 数据解析
- (void)createData
{
    
    self.Arr = [NSMutableArray array];
    NSMutableArray *tempArr = [NSMutableArray array];
    
//    NSString *bodyDic = @"auth=XMY4QH258ahDCMl0GsfqlDJxQ8qqYm2eO8GY6Cm1RjcWP23txPSR5c6v8&client=1&deviceid=27E1E230-C58B-4514-BB32-DF37127AC293&limit=10&start=0&version=3.0.3";
    
//     NSDictionary *bodyDic = [NSDictionary dictionaryWithObjectsAndKeys:@"client",@"1",@"deviceid",@"D8FDE94C-841F-4A4D-B5C4-AAD2472CEE85",@"auth",@"",@"version",@"3.0.1", nil];
    
     NSDictionary *bodyDic = [NSDictionary dictionaryWithObjectsAndKeys:@"auth",@"XMY4QH258ahDCMl0GsfqlDJxQ8qqYm2eO8GY6Cm1RjcWP23txPSR5c6v8", @"client",@"1", @"deviceid", @"27E1E230-C58B-4514-BB32-DF37127AC293", @"limit", @"10", @"start",@"0",@"version",@"3.0.3", nil];
    
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"application/x-javascript", nil];
    [manger POST:@"http://api2.pianke.me/pub/today" parameters:bodyDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject[@"data"];
        NSLog(@"%@", dic);
        NSArray *arr = dic[@"carousel"];
        for (NSDictionary *tempDic in arr) {
            adaptModel *model = [[adaptModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [tempArr addObject:model.img];
    
        }
        [self.Arr addObject:tempArr[tempArr.count - 1]];
        for (NSInteger i = 0; i < tempArr.count; i++) {
            [self.Arr addObject:tempArr[i]];
        }
        [self.Arr addObject:tempArr[0]];
        
        self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * self.Arr.count, 300);
        
        for (NSInteger i = 0; i < self.scrollView.subviews.count; i++) {
            UIImageView *imageView = self.scrollView.subviews[i];
            [imageView removeFromSuperview];
        }
        
        for (NSInteger i = 0; i < self.Arr.count; i++) {
            self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, 300)];
            
            [self.scrollView addSubview:self.imageView];
            
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.Arr[i]]];
            
            [self.imageView release];
        }
    
//        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
